using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 拖拽检索用的对象
/// </summary>
public class DragArea
{
    private bool isSelected = false;
    
    public Point DragP;
    public Square VisibleArea;

    public QuadTree tree;
    
    public DragArea(Vector3 center, Vector2 size)
    {
        DragP = new Point(false);
        DragP.pos = center;
        DragP.radius = QuadSetting.Instance.TestRadius;
        DragP.color = QuadSetting.Instance.VisibleCenterColor;

        VisibleArea = new Square();
        VisibleArea.center = center;
        VisibleArea.width = size.x;
        VisibleArea.height = size.y;
        VisibleArea.color = QuadSetting.Instance.VisibleAreaLine;
    }

    public void Init()
    {
        if (DragP == null)
            return;

        isSelected = false;
        DragP.OnMouseDown += OnMouseDown;
        DragP.OnMouseUp += OnMouseUp;
        DragP.OnMouseDrag += OnMouseDrag;
    }

    private void OnMouseDown(Event ent, Vector3 pos)
    {
        if (Vector3.Distance(DragP.pos, pos) < DragP.radius)
        {
            isSelected = true;
            DragP.color = QuadSetting.Instance.VisibleCenterDragColor;
        }
    }
    
    private void OnMouseUp(Event ent, Vector3 pos)
    {
        isSelected = false;
        DragP.color = QuadSetting.Instance.VisibleCenterColor;
    }

    private void OnMouseDrag(Event ent, Vector3 pos)
    {
        if (isSelected)
        {
            if (tree != null)
                tree.Search(VisibleArea.center, VisibleArea.Size);
            
            DragP.pos = pos;
            // update square center
            VisibleArea.center = pos;
        }
    }

    public void Draw()
    {
        DragP.Draw();
        VisibleArea.Draw();
    }
}
